﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using RooHui.Infrastructure.Entities.EFContext;

namespace RooHui.Infrastructure.Entities.Repository
{
    [Table("P_RepaymentPlan")]
    public class P_RepaymentPlan : IEntity<P_RepaymentPlan>
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int ID { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(128)]
        public string UserName { get; set; }
        public int ProjectId { get; set; }
        public int PolicyId { get; set; } 
        public decimal Rate { get; set; }
        public int CycleAmount { get; set; }
        public string StartDate { get; set; }
        public decimal RepaymentInterest { get; set; }
        public decimal RepaymentPrincipal { get; set; }
        public decimal RepaymentedInterest { get; set; }
        public decimal RepaymentedPrincipal { get; set; }
        public int SurplusDate { get; set; }
        public string EndDate { get; set; }
        public DateTime CreateTime { get; set; }
        public int Status { get; set; }

        public override P_RepaymentPlan Find(string key)
        {
            int Id = Int32.Parse(key);
            return Find(c => c.ID == Id);
        }

        public override P_RepaymentPlan Find(System.Linq.Expressions.Expression<Func<P_RepaymentPlan, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.RepaymentPlan.FirstOrDefault(where);
                if (item != null)
                {
                    this.CreateTime = item.CreateTime;
                    this.CycleAmount = item.CycleAmount;
                    this.EndDate = item.EndDate;
                    this.ID = item.ID;
                    this.PolicyId = item.PolicyId;
                    this.ProjectId = item.ProjectId; 
                    this.Rate = item.Rate;
                    this.RepaymentedInterest = item.RepaymentedInterest;
                    this.RepaymentedPrincipal = item.RepaymentedPrincipal;
                    this.RepaymentInterest = item.RepaymentInterest;
                    this.RepaymentPrincipal = item.RepaymentPrincipal;
                    this.StartDate = item.StartDate;
                    this.Status = item.Status;
                    this.SurplusDate = item.SurplusDate;
                    this.UserName = item.UserName;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<P_RepaymentPlan, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.RepaymentPlan.Any(where);
            }
        }
    }
}